var csInterface = new CSInterface();

// 个人主页
var homePageElement = document.querySelector("#homePage");
homePageElement.addEventListener("click", callHomepage);

function callHomepage() {
  window.cep.util.openURLInDefaultBrowser('https://gitee.com/brzjomo');
}

//获取尺寸
var runButtonCopySize = document.querySelector("#run-button-copySize");
runButtonCopySize.addEventListener("click", copySize);

function copySize() {
  const path = csInterface.getSystemPath(SystemPath.EXTENSION);
  csInterface.evalScript(`$.evalFile("${path}/areaToolMatVersion/jsx/copySize.jsx")`);

  csInterface.evalScript("copySize()");
}

//调用面积计算函数
var runButtonCalculateArea = document.querySelector("#run-button-calculateArea");
runButtonCalculateArea.addEventListener("click", preCalculateArea);

function preCalculateArea() {
  // 获取预设信息
  var presetElement = document.getElementById("preset");
  var preset = parseFloat(
    presetElement.options[presetElement.selectedIndex].value
  );

  // 获取宽幅
  var materialWidthElement = document.getElementById("material-width");
  var materialWidth = parseFloat(materialWidthElement.options[materialWidthElement.selectedIndex].value);

  // 获取制图比例
  var drawingScaleElement = document.getElementById("drawing-scale");
  var drawingScale = parseInt(drawingScaleElement.options[drawingScaleElement.selectedIndex].value);

  // 获取数据精度
  var dataAccuracyElement = document.getElementById("data-accuracy");
  var dataAccuracy = parseInt(dataAccuracyElement.options[dataAccuracyElement.selectedIndex].value);

  // 获取乘数，即张数
  var multiplier = parseInt(document.getElementsByName("text-multiply")[0].value);

  // 获取材料厚度
  var materialThicknessElement = document.getElementById("material-thickness");
  var materialThickness = parseInt(materialThicknessElement.options[materialThicknessElement.selectedIndex].value);

  // 获取单位
  var unitElement = document.getElementById("unit");
  var unit = parseInt(unitElement.options[unitElement.selectedIndex].value);

  // 获取材质
  var materialElement = document.getElementById("material");
  var material = parseInt(materialElement.options[materialElement.selectedIndex].value);

  // 获取标号
  var serialNumber = document.getElementsByName("serial-number")[0].value;
  var rule = /^\d+$/;
  if (rule.test(serialNumber)) {
    serialNumber = parseInt(serialNumber);
  } else {
    alert("编号只能为数字，也不能带任何符号！");
  }

  // 获取布尔-是否提示结果
  var alertResultElement = document.getElementById("isAlertResult");
  var isAlertResult = alertResultElement.checked;

  // 获取布尔-是否插入创建时间戳
  var creationTimeStampElement = document.getElementById("ifInsertCreationTimeStamp");
  var ifInsertCreationTimeStamp = creationTimeStampElement.checked;

  // 将宽幅和制图比例传递给函数
  csInterface.evalScript(
    "calculateArea(" +
      preset +
      ", " +
      materialWidth +
      ", " +
      drawingScale +
      ", " +
      dataAccuracy +
      ", " +
      multiplier +
      ", " +
      materialThickness +
      ", " +
      unit +
      ", " +
      material +
      ", " +
      serialNumber +
      ", " +
      isAlertResult +
      ", " +
      ifInsertCreationTimeStamp +
      ")"
  );
}
